// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Eggman Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 60 : TYPE_EGGMAN

// Function declarations
reserve function OOZEggman_Function135

// Static Values

// Tables

function OOZEggman_Function135
	Sin256(object.ypos, object.value2)
	object.ypos <<= 9
	object.ypos += object.value1
	object.value2 += 4
	object.value2 &= 255
end function


event ObjectMain
	switch object.state
	case 0
		if object[0].xpos > object.xpos
			temp0 = screen.xcenter
			FlipSign(temp0)
			if temp0 > -192
				temp0 = -192
			end if
			temp0 += object.ixpos
			stage.newXBoundary1 = temp0
			temp0 = screen.xcenter
			if temp0 < 192
				temp0 = 192
			end if
			temp0 += object.ixpos
			stage.newXBoundary2 = temp0
			temp0 = stage.newYBoundary2
			temp0 -= screen.ysize
			stage.newYBoundary1 = temp0
			object.value6 = object.ypos
			object.value6 -= 0x400000
			object.value7 = object.ypos
			object.value7 -= 0x440000
			object.value8 = object.ypos
			object.animation = 1
			ResetObjectEntity(26, TypeName[Music Event], 0, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			object.value3 = 8
			object.priority = PRIORITY_ACTIVE
			object.state = 1
		end if
		break
	case 1
		object.value0++
		if object.value0 == 60
			object.value0 = 0
			object.yvel = -0x8000
			if object[0].xpos > object.xpos
				object.direction = FLIP_NONE
			else
				object.direction = FLIP_X
			end if
			object.state = 2
		end if
		break
	case 2
		object.ypos += object.yvel
		if object.ypos <= object.value6
			object.ypos = object.value6
			object.value1 = object.ypos
			object.value2 = 0
			object.state = 3
		end if
		if object.animation == 1
			object.animationTimer = 0
		end if
		break
	case 3
		CallFunction(OOZEggman_Function135)
		object.value0++
		if object.value4 > 0
			object.value0 = 168
		end if
		if object.value0 == 168
			object.value0 = 0
			object.yvel = -0x4000
			object.state = 4
		end if
		break
	case 4
		object.ypos += object.yvel
		if object.ypos < object.value7
			object.ypos = object.value7
			object.yvel = 0x8000
		end if
		if object.ypos >= object.value8
			object.ypos = object.value8
			object.yvel = -0x8000
			if object[0].xpos > object.xpos
				object.direction = FLIP_NONE
			else
				object.direction = FLIP_X
			end if
			if object.value9 == 1
				temp0 = object.entityPos
				temp0++
				ResetObjectEntity(temp0, TypeName[Eggman Harpoon], object.direction, object.xpos, object.ypos)
				object[+1].ypos -= 0x300000
				object[+1].priority = PRIORITY_ACTIVE
				object.state = 5
			else
				temp0 = object.entityPos
				temp0++
				ResetObjectEntity(temp0, TypeName[Eggman Cannon], object.direction, object.xpos, object.ypos)
				object[+1].ypos -= 0x200000
				object[+1].priority = PRIORITY_ACTIVE
				object.state = 6
			end if
			object.visible = 0
			object.value9 = 0
		end if
		if object.animation == 1
			object.animationTimer = 0
		end if
		break
	case 5
		if object[+1].type == TypeName[Blank Object]
			temp0 = object.entityPos
			temp0++
			ResetObjectEntity(temp0, TypeName[Eggman Cannon], object.direction, object.xpos, object.ypos)
			object[+1].ypos -= 0x200000
			object[+1].priority = PRIORITY_ACTIVE
			object.state = 6
		end if
		break
	case 6
		if object[+1].type == TypeName[Blank Object]
			object.visible = 1
			object.state = 2
		end if
		break
	case 7
		object.value0++
		if object.value0 == 180
			object.value0 = 0
			object.animation = 5
			object.value5 = 0
			object.state = 8
			ResetObjectEntity(26, TypeName[Music Event], 1, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
		end if
		break
	case 8
		object.value0++
		if object.value0 == 30
			object.yvel = 0
			object.value0 = 0
			object.state = 9
		end if
		break
	case 9
		if object.yvel < 0x10000
			object.yvel += 0x400
		end if
		object.ypos += object.yvel
		if object.ypos > object.value8
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)
		end if
		break
	end switch
	if object.value3 != 0
		if object.value4 > 0
			object.value4--
			GetBit(temp0, object.value4, 0)
			if temp0 == 1
				SetPaletteEntry(0, 192, 0xE0E0E0)
			else
				SetPaletteEntry(0, 192, 0)
			end if
		end if
		if object.visible == 1
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				if object.value4 < 32
					CheckEqual(object[currentPlayer].animation, ANI_HURT)
					temp0 = checkResult
					CheckEqual(object[currentPlayer].animation, ANI_DYING)
					temp0 |= checkResult
					CheckEqual(object[currentPlayer].animation, ANI_DROWNING)
					temp0 |= checkResult
					if temp0 != 0
						if object.animation != 2
							object.animation = 2
							object.animationTimer = 0
						end if
					end if
					BoxCollisionTest(C_TOUCH, object.entityPos, -24, -10, 24, 24, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
					if checkResult == 1
						CallFunction(PlayerObject_CheckHit)
						if checkResult == 1
							object.value3--
							if object.value3 == 0
								player.score += 1000
								object.value0 = 0
								object.animation = 4
								object.animationTimer = 0
								object.value5 = 1
								object.value1 = object.ypos
								object.state = 7
								SetPaletteEntry(0, 192, 0)
							else
								object.animation = 3
								object.animationTimer = 0
								object.value4 = 64
								object.value9 = 1
								PlaySfx(SfxName[Boss Hit], 0)
							end if
						end if
					end if
				end if
			next
		end if
	end if
	if object.state != 9
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = object[currentPlayer].collisionRight
			temp0 <<= 16
			temp0 += object[currentPlayer].xpos
			temp1 = stage.curXBoundary2
			temp1 <<= 16
			if temp0 > temp1
				object[currentPlayer].xvel = 0
				object[currentPlayer].speed = 0
				object[currentPlayer].xpos = temp1
				temp0 = object[currentPlayer].collisionRight
				temp0 <<= 16
				object[currentPlayer].xpos -= temp0
			end if
		next
	end if
	if object.value5 == 1
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			Rand(temp0, 48)
			temp0 -= 24
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			object[tempObjectPos].drawOrder = 5
			PlaySfx(SfxName[Explosion], 0)
		end if
	end if
end event


event ObjectDraw
	DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)
	switch object.animation
	case 1
		temp0 = object.animationTimer
		temp0 /= 40
		object.animationTimer++
		object.animationTimer %= 80
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.animationTimer
		temp0 &= 15
		temp0 >>= 3
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			if object[0].animation != ANI_DYING
				object.animation = 1
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			object.animation = 1
		end if
		DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
		break
	case 4
		temp0 = object.animationTimer
		temp0 >>= 5
		temp0 += 4
		temp0 %= 5
		object.animationTimer++
		object.animationTimer &= 63
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 5
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break
	case 6
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
end event


event ObjectStartup
	SetPaletteEntry(0, 192, 0)
	CheckCurrentStageFolder("Zone07")
	if checkResult == 1
			if saveRAM[495] == 0
				LoadSpriteSheet("OOZ/Objects.gif")
			else
				LoadSpriteSheet("OOZ/ObjectsE.gif")
			endif
		SpriteFrame(-12, -12, 24, 12, 462, 118)
		SpriteFrame(-12, -12, 24, 12, 487, 118)
		SpriteFrame(-12, -12, 24, 12, 462, 131)
		SpriteFrame(-12, -12, 24, 12, 487, 131)
		SpriteFrame(-12, -12, 24, 12, 462, 144)
		SpriteFrame(-12, -12, 24, 12, 487, 144)
		SpriteFrame(-48, -32, 96, 64, 365, 125)
		if options.vsMode == 1
			foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)
				object[arrayPos0].type = TypeName[Blank Object]
			next
		end if
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-12, -12, 24, 12, 413, 91)
		SpriteFrame(-12, -12, 24, 12, 438, 91)
		SpriteFrame(-12, -12, 24, 12, 413, 104)
		SpriteFrame(-12, -12, 24, 12, 438, 104)
		SpriteFrame(-12, -12, 24, 12, 413, 117)
		SpriteFrame(-12, -12, 24, 12, 438, 117)
		SpriteFrame(-48, -32, 96, 64, 313, 112)
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
